package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }

    /**
     * 分页查询数据
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDTO dto) {
        //1.校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数无效!");
        }
        dto.checkParam(); // 检查分页
        //2.封装条件,执行查询
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        //3.敏感词名称判断
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //4.分页
        Page<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        //查询
        IPage<AdSensitive> iPage = this.page(page, wrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
    }
    /**
     * 新增敏感词淑数据
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数校验
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数无效!");
        }
        //2.判断敏感词是否存在
        if(StringUtils.isNotBlank(adSensitive.getSensitives())){
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在!");

            }
         //3.设置时间
            adSensitive.setCreatedTime(new Date());
            //4.插入数据
            save(adSensitive);
            return ResponseResult.okResult();
        }
        return null;
    }
    /**
     * 修改敏感词数据
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.参数校验
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数无效!");
        }
        //2.判断敏感词是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在!");
        }
        updateById(adSensitive);
        return ResponseResult.okResult();
    }
    /**
     * 删除敏感词淑数据
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数无效!");
        }
        removeById(id);
        return ResponseResult.okResult();
    }
}
